package com.huaao.micropolice.root.controllers.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.huaao.micropolice.common.util.JsonUtil;
import com.huaao.micropolice.core.model.system.Dict;
import com.huaao.micropolice.core.model.system.Menu;
import com.huaao.micropolice.core.model.system.Role;
import com.huaao.micropolice.core.service.system.DictService;
import com.huaao.micropolice.core.service.system.MenuService;
import com.huaao.micropolice.core.service.system.RoleService;
import com.huaao.micropolice.root.commons.TreeUtils;
import com.huaao.micropolice.root.controllers.BaseController;
import com.huaao.micropolice.root.controllers.IndexController;

@Controller
@RequestMapping("system/menu")
public class MenuController extends BaseController{
	
	private static final Logger log = Logger.getLogger(IndexController.class);
	
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private MenuService menuService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private DictService dictService;
	@Value("${ico}")
	private String ico;
	
	@RequestMapping("save.do")
    public ResponseEntity<String> save(Menu menu) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			menuService.save(menu);
			map = successMap();
			log.info(menu.getId());
		} catch (Exception e) {
			map = failMap();
			log.error(e.getMessage());
		}
		return super.ajaxModel(JSON, JsonUtil.toJson(map));
    }
	
	@RequestMapping("dels.do")
	public ResponseEntity<String> dels(@RequestParam(value = "ids[]") String[] ids){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String message = menuService.dels(ids);
			map = successMap(message);
		} catch (Exception e) {
			map = failMap();
		}
		return super.ajaxModel(JSON, JsonUtil.toJson(map));
	}
	
	@RequestMapping("findAllJson.do")
	public ResponseEntity<String> findAll(){
		Map<String, Object> map = new HashMap<String, Object>();
		List<Menu> menus = menuService.findAll();
		StringBuffer buff = new StringBuffer();
		TreeUtils.buildMenuTreeJson(buff,menus);
		map.put("result", buff.toString());
		return super.ajaxModel(JSON, JsonUtil.toJson(map));
	}
	
	@RequestMapping("findAllJsonByRole.do")
	public ResponseEntity<String> findAllJsonByRole(String roleId){
		Map<String, Object> map = new HashMap<String, Object>();
		List<Menu> menus = menuService.findAll();
		Role role = roleService.load(roleId);
		StringBuffer buff = new StringBuffer();
		TreeUtils.buildMenuTreeJson(buff,menus,role.getMenus());
		map.put("result", buff.toString());
		return super.ajaxModel(JSON, JsonUtil.toJson(map));
	}

	@RequestMapping("index.do")
	public String test_list(Model modelMap){
		List<Menu> list = menuService.findRoot();
		List<Dict> dicts = dictService.queryDictType(ico);
		modelMap.addAttribute("dicts",dicts);
		modelMap.addAttribute("menus", list);
		return "/system/menu/index";
	}
}